package com.txtsqlclient.ui.si;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.txtsqlclient.dataaccess.database.connection.ConnectionException;
import com.txtsqlclient.dataaccess.database.connection.ConnectionFactory;
import com.txtsqlclient.domain.Database;
import com.txtsqlclient.ui.Messages;
import com.txtsqlclient.ui.exception.SubInterfaceException;
import com.txtsqlclient.ui.helper.UserInputCapturer;
import com.txtsqlclient.ui.vo.Session;

/**
 * @author spadda
 * 
 */
public class DatabaseInfoSubInterface extends PromptBasedSubInterface {

	private String ip;

	private String database;

	private String user;

	private String password;

	private final static Log LOGGER = LogFactory
			.getLog(DatabaseInfoSubInterface.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.ui.InputGatherer#gatherInput(com.txtsqlclient.ui.Session)
	 */
	public void gatherInput(Session session) throws SubInterfaceException {
		try {
			readIP();
			readDatabase();
			readUser();
			readPassword();
		} catch (IOException ie) {
			throw new SubInterfaceException(
					"Error while reading user input from console:", ie);
		}

	}

	protected void readIP() throws IOException {
		this.ip = UserInputCapturer
				.getLine("Please enter the IP/hostname of the database server:");
		if (this.ip == null || this.ip.length() == 0) {
			System.out.println(Messages.IP_ERROR.toUpperCase());
			// Try to read IP again.
			readIP();
		}
	}

	protected void readDatabase() throws IOException {
		this.database = UserInputCapturer
				.getLine("Please enter the database instance to connect to:");
	}

	protected void readUser() throws IOException {
		this.user = UserInputCapturer.getLine("Please enter the username:");
		if (this.user == null || this.user.length() == 0) {
			System.out.println(Messages.USER_ERROR.toUpperCase());
			// Try to read user again.
			readUser();
		}
	}

	protected void readPassword() throws IOException {
		this.password = new String(UserInputCapturer
				.getSecureMaskedLine("Please enter the password: "));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.ui.InputHandler#handleInput(com.txtsqlclient.ui.Session)
	 */
	public void handleInput(Session session) throws SubInterfaceException {
		List<String> errors = new ArrayList<String>();
		if (session.getDsType() == null) {
			errors.add(Messages.DSTYPE_ERROR);
		}
		if (this.ip == null || this.ip.trim().length() == 0) {
			errors.add(Messages.IP_ERROR);
		}
		if (this.user == null || this.user.trim().length() == 0) {
			errors.add(Messages.USER_ERROR);
		}
		if (!errors.isEmpty()) {
			throw new IllegalArgumentException(Arrays
					.toString(errors.toArray()));
		}

		Database database = new Database(session.getDsType(), this.ip,
				this.database, this.user, this.password);
		try {
			ConnectionFactory.getInstance().getConnection(database);
			LOGGER.debug("Obtained connection to the data source.");
			session.setDataSource(database);
		} catch (ConnectionException e) {
			throw new SubInterfaceException(
					"Problem connection to database with the given details. Please verify login information, or try again later",
					e);
		}
	}
}